package com.j.thread;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class ThreadTest {
    public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException {

        MyThread myThread = new MyThread();
        myThread.start();

        MyRunnable myRunnable = new MyRunnable();
        myRunnable.run();

        //给thread传入一个实例化的MyRunnable,那么运行的时候就会运行，该实例化的run方法
        Thread thread1 = new Thread(myRunnable);
        thread1.run();
        //把所有任务放入线程池
        List<Callable<Integer>> callables = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            MyCallable myCallable = new MyCallable(i);
            callables.add(myCallable);
        }
        //创建一个10个线程的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        //线程池执行线程，1秒钟时间
        List<Future<Integer>> futures = executorService.invokeAll(callables, 1000, TimeUnit.SECONDS);
        for (Future<Integer> future : futures) {
            Integer integer = future.get();
            System.out.println(integer);
        }
        //并且这里线程是不会退出的,需要手动退出
        executorService.shutdown();

    }
}
